const Router = require('koa-router');
const community = new Router();
const sqldata = require('./sqldata');

community.get('/', async (ctx)=>{
    //ctx.body = "社区-推荐页-汪汪星球";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang = `select * from works,user
        where works.Uid = user.Uid and Wtag="#汪汪星球"`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    console.log(mydata)
    ctx.body = mydata;
})

community.get('/focus', async (ctx)=>{
    //ctx.body = "社区-关注页";
    let mydata = await new Promise((resolve, reject)=>{
        var val=ctx.request.query;
        let sqlLang = `select * from works,user where works.Uid in (select Bid from attention where Uid='${val.Uid}') and works.Uid=user.Uid`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})

community.get('/city', async (ctx)=>{
    //ctx.body = "社区-同城页";
    let mydata = await new Promise((resolve, reject)=>{
        var val=ctx.request.query;
        let sqlLang = `select * from works,user
        where works.Uid = user.Uid and Wcity like '${val.Wcity}%'`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})


community.get('/detail', async (ctx)=>{
    //ctx.body = "社区-详情页";
    let mydata;
    let mydata1;
    let mydata2;
    let sql;
    var val=ctx.request.query;
    console.log(val)
    mydata = await new Promise((resolve, reject)=>{
        sql = `select * from works,user
        where works.Uid = user.Uid and works.Wid ='${val.detailnum}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    for(let k=0;k<mydata.length;k++){
        let time=mydata[k].Wtime
        let year = time.getFullYear()
        let month = time.getMonth() + 1
        let date = time.getDate()
        let hours = time.getHours()
        let minute = time.getMinutes()
        let second = time.getSeconds()
        if (month < 10) { month = '0' + month }
        if (date < 10) { date = '0' + date }
        if (hours < 10) { hours = '0' + hours }
        if (minute < 10) { minute = '0' + minute }
        if (second < 10) { second = '0' + second }
        mydata[k].Wtime=year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    }
    mydata1=await new Promise((resolve, reject)=>{
        sql = `select * from attented,works where works.Wid ='${val.detailnum}' and works.Uid=attented.Uid and attented.Bid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2=await new Promise((resolve, reject)=>{
        sql = `select * from workfabulous where workfabulous.Wid ='${val.detailnum}' and workfabulous.Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(mydata1==''){
        mydata[0].Astate='+关注'
    }else{
        mydata[0].Astate='已关注'
    }
    if(mydata2==''){
        mydata[0].img='https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'
    }else{
        mydata[0].img='https://s3.bmp.ovh/imgs/2021/11/2d7cde86aa7b7457.png'
    }
    ctx.body = mydata;
})
community.post('/detail',async(ctx)=>{
    console.log(ctx.request.body)
    let val=ctx.request.body;
    let mydata1;
    let mydata2;
    let mydata3;
    let mydata4;
    let sql;
    mydata4= await new Promise((resolve, reject)=>{
        sql=`select Uid from works where Wid='${val.detailnum}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    console.log(mydata4)
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${mydata4[0].Uid}' and Bid='${val.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2 = await new Promise((resolve, reject)=>{
        sql=`select * from attented where Uid='${val.Uid}' and Bid='${mydata4[0].Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(val.Astate=='已关注'){
        if(mydata1==''&& mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${mydata4[0].Uid}','${val.Uid}','${val.Time}','+关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata1=='' && mydata2!==''){
            mydata3= await new Promise((resolve, reject)=>{
                sql=`insert into attented values ('${mydata4[0].Uid}','${val.Uid}','${val.Time}','已关注')`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${mydata4[0].Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3= await new Promise((resolve, reject)=>{
            sql=`insert into attention values ('${val.Uid}','${mydata4[0].Uid}','${val.Astate}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{      
        if(mydata2==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${mydata4[0].Uid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }else if(mydata2!==''){
            mydata3 = await new Promise((resolve, reject)=>{
                sql=`delete from attented where Uid='${mydata4[0].Uid}' and Bid='${val.Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
            mydata3= await new Promise((resolve, reject)=>{
                sql=`update attented  set Time='${val.Time}',Astate='${val.Astate}' where Uid='${val.Uid}' and Bid='${mydata4[0].Uid}'`
                return sqldata(sql, (err, data)=>{
                    if(err) reject(err);
                    resolve(data);	// 返回拿到的数据
                })
            })
        }
        mydata3 = await new Promise((resolve, reject)=>{
            sql=`delete from attention where Uid='${val.Uid}' and Bid='${mydata4[0].Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
})
community.get('/lookComment', async (ctx)=>{
    var val=ctx.request.query;
    console.log(val)
    let sqlLang 
    let mydata1;
    let mydata2;
    if(val.btn=='work'){
        mydata1 = await new Promise((resolve, reject)=>{
            sqlLang= `select * from comment,user
            where comment.Wid ='${val.detailnum}' and user.Uid=comment.Uid`;
            return sqldata(sqlLang, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata2= await new Promise((resolve, reject)=>{
            sqlLang= `select * from comment,commentfabulous
            where comment.Wid ='${val.detailnum}' and comment.Cid=commentfabulous.Cid and commentfabulous.Uid='${val.Uid}'`;
            return sqldata(sqlLang, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{
        mydata1 = await new Promise((resolve, reject)=>{
            sqlLang= `select * from comment,user
            where comment.Wid ='${val.detailnum}' and user.Uid=comment.Uid`;
            return sqldata(sqlLang, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        mydata2= await new Promise((resolve, reject)=>{
            sqlLang= `select * from comment,commentfabulous
            where comment.Wid ='${val.detailnum}' and comment.Cid=commentfabulous.Cid and commentfabulous.Uid='${val.Uid}'`;
            return sqldata(sqlLang, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
   
    if(mydata2==''){
        for(let i=0;i<mydata1.length;i++){
            mydata1[i].img='https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'
        }
    }else{
        for(let j=0;j<mydata1.length;j++){
            mydata1[j].img='https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'
            for(let k=0;k<mydata2.length;k++){
                if(mydata1[j].Cid==mydata2[k].Cid){
                    mydata1[j].img='https://s3.bmp.ovh/imgs/2021/11/2d7cde86aa7b7457.png'
                }
            }
        }
    }
    ctx.body = mydata1;
})
community.get('/cat', async (ctx)=>{
    //ctx.body = "社区-喵喵星球";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang =  `select * from works,user
        where works.Uid = user.Uid and Wtag="#喵喵星球"`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
community.get('/else', async (ctx)=>{
    //ctx.body = "社区-其他星球";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang =  `select * from works,user
        where works.Uid = user.Uid and Wtag="#其他星球"`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
community.get('/search', async (ctx)=>{
    //ctx.body = "社区-搜索";
    let mydata = await new Promise((resolve, reject)=>{
        var val=ctx.request.query
        let sqlLang = `select * from works,user where works.Uid=user.Uid and (Wtitle like '%${val.inf}%' or Wintro like '%${val.inf}%')`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
module.exports = community;
